package com.gitee.hljdrl.dsbridge.client;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.view.Gravity;
import android.view.ViewGroup;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.gitee.hljdrl.dsbridge.basis.WebViewEvent;

public class DSWebChromeClient extends WebChromeClient {

    protected WebViewEvent mWebViewEvent;

    public DSWebChromeClient() {

    }
    public DSWebChromeClient(WebViewEvent webViewEvent) {
        mWebViewEvent = webViewEvent;
    }

    public void setWebViewEvent(WebViewEvent webViewEvent) {
        mWebViewEvent = webViewEvent;
    }

    @Override
    public boolean onJsAlert(WebView view, String url, final String message, final JsResult result) {
        if (mWebViewEvent == null) {
            return super.onJsAlert(view, url, message, result);
        }
        if (!mWebViewEvent.isAlertBoxBlock()) {
            result.confirm();
        }

        Dialog alertDialog = new AlertDialog.Builder(mWebViewEvent.getContext()).
                setMessage(message).
                setCancelable(false).
                setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (mWebViewEvent.isAlertBoxBlock()) {
                            result.confirm();
                        }
                    }
                })
                .create();
        alertDialog.show();
        return true;
    }

    @Override
    public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
        if (mWebViewEvent == null) {
            return onJsConfirm(view, url, message, result);
        }
        if (!mWebViewEvent.isAlertBoxBlock()) {
            result.confirm();
        }

        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (mWebViewEvent.isAlertBoxBlock()) {
                    if (which == Dialog.BUTTON_POSITIVE) {
                        result.confirm();
                    } else {
                        result.cancel();
                    }
                }
            }
        };
        new AlertDialog.Builder(mWebViewEvent.getContext())
                .setMessage(message)
                .setCancelable(false)
                .setPositiveButton(android.R.string.ok, listener)
                .setNegativeButton(android.R.string.cancel, listener).show();
        return true;


    }

    @Override
    public boolean onJsPrompt(WebView view, String url, final String message, String defaultValue, final JsPromptResult result) {
        if (mWebViewEvent == null) {
            return onJsPrompt(view, url, message, defaultValue, result);
        }

        if (!mWebViewEvent.isAlertBoxBlock()) {
            result.confirm();
        }
        final Context context = mWebViewEvent.getContext();

        final EditText editText = new EditText(context);
        editText.setText(defaultValue);
        if (defaultValue != null) {
            editText.setSelection(defaultValue.length());
        }
        float dpi = context.getResources().getDisplayMetrics().density;
        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (mWebViewEvent.isAlertBoxBlock()) {
                    if (which == Dialog.BUTTON_POSITIVE) {
                        result.confirm(editText.getText().toString());
                    } else {
                        result.cancel();
                    }
                }
            }
        };
        new AlertDialog.Builder(context)
                .setTitle(message)
                .setView(editText)
                .setCancelable(false)
                .setPositiveButton(android.R.string.ok, listener)
                .setNegativeButton(android.R.string.cancel, listener)
                .show();
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        int t = (int) (dpi * 16);
        layoutParams.setMargins(t, 0, t, 0);
        layoutParams.gravity = Gravity.CENTER_HORIZONTAL;
        editText.setLayoutParams(layoutParams);
        int padding = (int) (15 * dpi);
        editText.setPadding(padding - (int) (5 * dpi), padding, padding, padding);
        return true;

    }
}
